package com.shd.edanpei.presenter;

import android.os.Bundle;
import android.text.TextUtils;

import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.data.OrderConstant;
import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.CustomerBean;
import com.shd.edanpei.bean.ImgInfoBean;
import com.shd.edanpei.bean.PicBean;
import com.shd.edanpei.presenter.contract.FileListContract;
import com.shd.edanpei.util.GsonUtil;
import com.shd.edanpei.util.ToastUtil;

import java.util.ArrayList;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/4/4.
 * yang-wen-wen
 */

public class FileListPresenter extends BasePresenterImpl<FileListContract.FileListIView>
        implements FileListContract.FileListIPresenter {

    public FileListPresenter(FileListContract.FileListIView mView) {
        super(mView);
    }
    private CustomerBean bean = new CustomerBean();
    @Override
    public void getOrderInfo(final int orderID) {
        ApiCreator.getApi().getOrderInfo(orderID)
                .compose(RxTransformer.<CustomerBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<CustomerBean>() {
                    @Override
                    protected void onSuccess(CustomerBean data) {
                        bean.setOrderId(orderID + "");
                        bean = data;
                        mBaseIView.setData(data);
                    }
                });
    }

    @Override
    public void getFileData(int orderID, final Bundle bundle) {
        ApiCreator.getApi().getFileData(orderID)
                .compose(RxTransformer.<ImgInfoBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ImgInfoBean>() {
                    @Override
                    protected void onSuccess(ImgInfoBean data) {
                        ArrayList<String> list = new ArrayList<>();
                        list.add("");
//                        data.setOtherHouseCert(list);
                        if (data.getMaritalCert().size() == 0 && bundle.getBoolean("isMarry")) {
                            data.setMaritalCert(list);
                        }
                        if (data.getCarCert().size() == 0 && bundle.getBoolean("isCar")) {
                            data.setCarCert(list);
                        }
                        if (data.getOtherHouseCert().size() == 0 && bundle.getBoolean("other")) {
                            data.setOtherHouseCert(list);
                        }
                        if (data.getBusinessCert().size() == 0 && bundle.getBoolean("company")) {
                            data.setBusinessCert(list);
                        }
                        mBaseIView.setUI(data);
                    }
                });
    }

    @Override
    public void submitCarFile(PicBean picBean, CustomerBean customerBean) {
        picBean.setIdCardPic(OrderConstant.ID_PIC);
        Map<String, Object> map = checkCarFile(picBean, customerBean);
        if (map != null) {
            mBaseIView.showLoading("");
            ApiCreator.getApi().submitCarFile(checkCarFile(picBean, customerBean))
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            //订单创建成功
                           // ToastUtil.showToast("您提交的资料已经成功受理，正在通过后台审核");
                            ToastUtil.showToast("订单发布成功");
                            RxBus.get().send(RxBusCode.ORDER_CREATE_SUCCESS);
                            mBaseIView.getContext().finish();
                        }

                        @Override
                        public void onFinish() {
                            mBaseIView.stopLoading();
                        }
                    });
        }
    }

    @Override
    public void submitHouseFile(PicBean picBean, CustomerBean customerBean) {
        picBean.setIdCardPic(OrderConstant.ID_PIC);
        Map<String, Object> map = checkHouseFile(picBean, customerBean);
        if (map != null) {
            mBaseIView.showLoading("");
            ApiCreator.getApi().submitHouseFile(map)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            //订单创建成功
                            //ToastUtil.showToast("您提交的资料已经成功受理，正在通过后台审核");
                            ToastUtil.showToast("订单发布成功");
                            RxBus.get().send(RxBusCode.ORDER_CREATE_SUCCESS);
                            mBaseIView.getContext().finish();

                        }

                        @Override
                        public void onFinish() {
                            mBaseIView.stopLoading();
                        }
                    });
        }
    }

    @Override
    public void submitCreditAttachment(PicBean picBean, CustomerBean customerBean) {
        picBean.setIdCardPic(OrderConstant.ID_PIC);
        Map<String, Object> map = checkFilePersional(picBean, customerBean);
        if (map != null) {
            mBaseIView.showLoading("");
            ApiCreator.getApi().submitCreditAttachment(map)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            //订单创建成功
                           // ToastUtil.showToast("您提交的资料已经成功受理，正在通过后台审核");
                            ToastUtil.showToast("订单发布成功");
                            RxBus.get().send(RxBusCode.ORDER_CREATE_SUCCESS);
                            mBaseIView.getContext().finish();

                        }

                        @Override
                        public void onFinish() {
                            mBaseIView.stopLoading();
                        }
                    });
        }
    }

    @Override
    public void submitTaxrAttachment(PicBean picBean, CustomerBean customerBean) {
        picBean.setIdCardPic(OrderConstant.ID_PIC);
        Map<String, Object> map = checkFileEnterp(picBean, customerBean);
        if (map != null) {
            mBaseIView.showLoading("");
            ApiCreator.getApi().submitTaxrAttachment(map)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            //订单创建成功
                           // ToastUtil.showToast("您提交的资料已经成功受理，正在通过后台审核");
                            ToastUtil.showToast("订单发布成功");
                            RxBus.get().send(RxBusCode.ORDER_CREATE_SUCCESS);
                            mBaseIView.getContext().finish();

                        }

                        @Override
                        public void onFinish() {
                            mBaseIView.stopLoading();
                        }
                    });
        }
    }

    @Override
    public void updateCreditAttachment(PicBean picBean, int orderID) {
        picBean.setOrderId(orderID + "");
        Map<String, Object> map = GsonUtil.toMap(picBean);
        ApiCreator.getApi().updateCreditAttachment(map)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        ToastUtil.showToast("修改成功");
                        mBaseIView.getContext().finish();
                    }
                });
    }

    @Override
    public void updateTaxrAttachment(PicBean picBean, int orderID) {
        picBean.setOrderId(orderID + "");
        Map<String, Object> map = GsonUtil.toMap(picBean);
        ApiCreator.getApi().updateTaxrAttachment(map)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        ToastUtil.showToast("修改成功");
                        mBaseIView.getContext().finish();
                    }
                });
    }

    @Override
    public void updateCarFile(PicBean picBean, int orderID) {
        picBean.setOrderId(orderID + "");
        Map<String, Object> map = GsonUtil.toMap(picBean);
        ApiCreator.getApi().updateCarFile(map)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        ToastUtil.showToast("修改成功");
                        mBaseIView.getContext().finish();
                    }
                });
    }

    @Override
    public void updateHouseFile(PicBean picBean, int orderID) {
        picBean.setOrderId(orderID + "");
        Map<String, Object> map = GsonUtil.toMap(picBean);
        ApiCreator.getApi().updateHouseFile(map)
                .compose(RxTransformer.<NoDataResponse>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<NoDataResponse>() {
                    @Override
                    protected void onSuccess(NoDataResponse data) {
                        ToastUtil.showToast("修改成功");
                        mBaseIView.getContext().finish();
                    }
                });
    }

    private Map<String, Object> checkCarFile(PicBean picBean, CustomerBean customerBean) {
//        if (isNull(picBean.getIdCardPic(), "身份证未上传")) return null;
        if (isNull(picBean.getHouseholdRegister(), "户口本未上传")) return null;
//        if (isNull(picBean.getBankPic(), "银行卡未上传")) return null;
        if (isNull(picBean.getCarCert(), "车辆登记证/行驶证未上传")) return null;
        if (customerBean.isHaveCarInsurance()
                && isNull(picBean.getCarWarranty(), "车辆保单未上传")) return null;
        if (customerBean.isMarrage()
                && isNull(picBean.getMaritalCert(), "结婚证/未婚证未上传")) return null;
        if (isNull(picBean.getCreditCert(), "征信未上传")) return null;
        return GsonUtil.toMap(picBean);

    }

    private Map<String, Object> checkHouseFile(PicBean picBean, CustomerBean customerBean) {
//        if (isNull(picBean.getIdCardPic(), "身份证未上传")) return null;
        if (isNull(picBean.getHouseholdRegister(), "户口本未上传")) return null;
//        if (isNull(picBean.getBankPic(), "银行卡未上传")) return null;
        if (isNull(picBean.getRoomCert(), "房产证/不动产证未上传")) return null;
        if (customerBean.isHaveCompany()
                && isNull(picBean.getBusinessCert(), "公司营业执照未上传")) return null;
//        if (customerBean.isHaveCompany()
//                && isNull(picBean.getCompanyStream(), "公司流水未上传")) return null;
        if (customerBean.isHaveOtherHouse()
                && isNull(picBean.getOtherHouseCert(), "其他房产证未上传")) return null;
        if (customerBean.isMarrage()
                && isNull(picBean.getMaritalCert(), "结婚证/未婚证未上传")) return null;
        if (isNull(picBean.getCreditCert(), "征信未上传")) return null;
        return GsonUtil.toMap(picBean);

    }

    private Map<String, Object> checkFilePersional(PicBean picBean, CustomerBean customerBean) {
//        if (isNull(picBean.getIdCardPic(), "身份证未上传")) return null;
        if(customerBean.getInsurance().equals("1")) {
            if (isNull(picBean.getCarWarranty(), "保单信息未上传")) return null;
        }
//        if (isNull(picBean.getBankPic(), "银行卡未上传")) return null;
        if(customerBean.getHouse().equals("1")){
            if (isNull(picBean.getRoomCert(), "房产证/不动产证未上传")) return null;
        }

        if (isNull(picBean.getCreditCert(), "征信未上传")) return null;
        return GsonUtil.toMap(picBean);

    }

    private Map<String, Object> checkFileEnterp(PicBean picBean, CustomerBean customerBean) {

        if (customerBean.isHaveCompany()
                && isNull(picBean.getBusinessCert(), "公司营业执照未上传")) return null;

        if (isNull(picBean.getCreditCert(), "征信未上传")) return null;
        return GsonUtil.toMap(picBean);

    }

    private boolean isNull(String str, String toast) {
        if (TextUtils.isEmpty(str)) {
            ToastUtil.showToast(toast);
            return true;
        }
        return false;
    }


}
